﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace MedicWeb.forms {
    public partial class DoctorList : SessionPage {
        protected void Page_Load(object sender, EventArgs e) {
            ////////////////////////////////////
            var mc = new MainService();
            var settings = mc.GetSettings(GetSession());
            var alertTime = settings.AlertTime;
            int? prevYear = Session["docListYear"] as int?;
            if (!IsPostBack) {
                for (int i = 2006; i <= DateTime.Now.Year; i++) {
                    comboYear.Items.Add(new ListItem("" + i, "" + i));
                }
                comboYear.SelectedValue = "" + (prevYear ?? DateTime.Now.Year);
            }

            int year = Convert.ToInt32(comboYear.SelectedValue);
            List<Doctor> doctorList = Session["docList"] as List<Doctor>;
            if (doctorList == null || prevYear != year) {
                doctorList = new List<Doctor>();
                doctorList.AddRange(mc.GetDoctorList(GetSession(), year, false));
                Session["docList"] = doctorList;
                Session["docListYear"] = year;
            }
            Dictionary<int, Speciality> specs = new Dictionary<int, Speciality>();
            ///////////////////////////////////////////
            foreach (Doctor doctor in doctorList) {
                if (doctor.NewSpeciality != null
                    && doctor.NewSpeciality.Id > 0
                    && !specs.ContainsKey(doctor.NewSpeciality.Id)) {
                    specs.Add(doctor.NewSpeciality.Id, doctor.NewSpeciality);
                }
                var row = new TableRow();
                DateTime reg = doctor.RegstrationDate;
                DateTime now = DateTime.Now;
                TimeSpan diff = now - reg;
                if (alertTime > 0 && diff.TotalDays > 30 * alertTime && (doctor.Order == null || doctor.Order.Id == 0)) {
                    row.CssClass += " alert";
                }
                row.Attributes["speciality"] = doctor.NewSpeciality != null ? ("" + doctor.NewSpeciality.Id) : "";
                var cellEdit = new TableCell();
                var cellPrint = new TableCell();
                var cellFio = new TableCell();
                var cellJob = new TableCell();
                var cellInstiution = new TableCell();
                var cellOrder = new TableCell();
                var cellNumberInOrder = new TableCell();
                var cellProtNum = new TableCell();
                var cellProtDate = new TableCell();
                cellFio.Controls.Add(
                    new HyperLink() {
                        Text = doctor.Surname + " " + doctor.Name + " " + doctor.Patronymic,
                        NavigateUrl = ResolveUrl("~/forms/Editors/DoctorEdit.aspx?id=" + doctor.Id),
                        CssClass = "modal block"
                    });

                cellJob.Text = doctor.Job.Title;
                cellInstiution.Text = doctor.Institution.Title;
                if (doctor.Order != null && doctor.Order.Id > 0) {
                    cellOrder.Controls.Add(new HyperLink() {
                        Text = doctor.Order.Number,
                        NavigateUrl =
                            ResolveUrl("~/forms/Editors/OrderEdit.aspx?id=" + doctor.Order.Id),
                        CssClass = "modal block"
                    });
                    cellOrder.Controls.Add(new Label() { Text = "да", CssClass = "hidden" });
                } else {
                    cellOrder.Controls.Add(new Label() { Text = "нет", CssClass = "hidden" });
                }
                cellOrder.CssClass = "nowrap";
                //cellOrder.Text = doctor.Order.Number;
                cellNumberInOrder.Text = "" + doctor.NumberInOrder;
                cellEdit.Controls.Add(
                   new HyperLink() {
                       NavigateUrl = ResolveUrl("~/forms/Editors/DoctorEdit.aspx?id=" + doctor.Id),
                       CssClass = "modal edit"
                   });
                var panel = new Panel() { CssClass = "print" };
                cellPrint.Controls.Add(panel);
                panel.Controls.Add(
                    new HyperLink() {
                        NavigateUrl = ResolveUrl("~/forms/Reports/Print/Print.aspx?type=" + typeof(PrintAttestatProtocol) + "&id=" + doctor.Id),
                        //NavigateUrl = ResolveUrl("~/forms/Reports/Protocol/ReportAttestatProtocol.aspx?id=" + doctor.Id),
                        Text = "Протокол(Аттестационная комиссия)",
                        CssClass = "print"
                    });
                panel.Controls.Add(
                    new HyperLink() {
                        NavigateUrl = ResolveUrl("~/forms/Reports/Print/Print.aspx?type=" + typeof(PrintProfileProtocol) + "&id=" + doctor.Id),
                        //NavigateUrl = ResolveUrl("~/forms/Reports/Protocol/ReportProfileProtocol.aspx?id=" + doctor.Id),
                        Text = "Протокол(Профильная комиссия)",
                        CssClass = "print"
                    });
                panel.Controls.Add(
                        new HyperLink()
                        {
                            NavigateUrl = ResolveUrl("~/forms/Reports/Invoice/ReportInvoice.aspx?id=" + doctor.Id),
                            Text = "Квитанция на оплату",
                            CssClass = "print"
                        });
                panel.Controls.Add(
                        new HyperLink()
                        {
                            NavigateUrl = ResolveUrl("~/forms/Reports/Print/Print.aspx?type=complex&id=" + doctor.Id),
                            //NavigateUrl = ResolveUrl("~/forms/Reports/Sertificate/ReportSertificate.aspx?id=" + doctor.Id),
                            Text = "Комплексная печать документов",
                            CssClass = "print"
                        });
                if (doctor.Order != null && doctor.Order.Id > 0) {
                    panel.Controls.Add(
                        new HyperLink() {
                            NavigateUrl = ResolveUrl("~/forms/Reports/Print/Print.aspx?type=" + typeof(PrintExtract) + "&id=" + doctor.Id),
                            //NavigateUrl = ResolveUrl("~/forms/Reports/Extract/ReportExtract.aspx?id=" + doctor.Id),
                            Text = "Выписка",
                            CssClass = "print"
                        });
                    
                    panel.Controls.Add(
                        new HyperLink() {
                            NavigateUrl = ResolveUrl("~/forms/Reports/Print/Print.aspx?type=" + typeof(PrintSertificate) + "&id=" + doctor.Id),
                            //NavigateUrl = ResolveUrl("~/forms/Reports/Sertificate/ReportSertificate.aspx?id=" + doctor.Id),
                            Text = "Удостоверение",
                            CssClass = "print"
                        });
                    
                }
                if (doctor.Order != null) {
                    cellProtNum.Text = !string.IsNullOrEmpty(doctor.Order.ProtocolNumber) ? doctor.Order.ProtocolNumber : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    cellProtDate.Text = doctor.Order.ProtocolDate != null ? doctor.Order.ProtocolDate.Value.ToShortDateString() : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                }

                cellEdit.Width = 24;
                cellPrint.Width = 24;

                row.Cells.Add(cellPrint);
                row.Cells.Add(cellEdit);
                row.Cells.Add(createDeleteCell(doctor));
                row.Cells.Add(cellFio);
                row.Cells.Add(cellJob);
                row.Cells.Add(cellInstiution);
                row.Cells.Add(cellOrder);
                row.Cells.Add(cellNumberInOrder);
                row.Cells.Add(cellProtNum);
                row.Cells.Add(cellProtDate);

                //row.Attributes["onclick"] = "link('~/forms/Editors/DoctorEdit.aspx?id=" + doctor.Id.ToString() + "');";

                TableDoctors.Rows.Add(row);
            }
            comboSpecFilter.CssClass = "nullable";
            comboSpecFilter.Items.Add(new ListItem("- не выбрано -", "null"));
            var specialities = specs.Values.OrderBy(x => x.Title).ToArray();
            foreach (var sk in specialities) {
                comboSpecFilter.Items.Add(new ListItem(sk.Title, "" + sk.Id));
            }
            if (!IsPostBack) {
                comboSpecFilter.SelectedValue = "null";
            }
            //Region[] regionList = mc.GetRegionList(GetSession());
            //foreach (var region in regionList) {
            //    var nodeRegion = new TreeNode(region.Title, "" + region.Id);
            //    Locality[] localityListByRegion = mc.GetLocalityListByRegion(GetSession(), region);
            //    foreach (var locality in localityListByRegion) {
            //        var nodeLocality = new TreeNode(locality.Title, "" + locality.Id);
            //        Institution[] institutions = mc.GetInstitutionListByLocality(GetSession(), locality);
            //        foreach (var institution in institutions) {
            //            var nodeInstitution = new TreeNode(institution.Title, "" + institution.Id);
            //            nodeInstitution.ToolTip = institution.Title;
            //            nodeLocality.ChildNodes.Add(nodeInstitution);
            //        }
            //        nodeRegion.ChildNodes.Add(nodeLocality);
            //        nodeLocality.SelectAction = TreeNodeSelectAction.None;
            //    }
            //    nodeRegion.SelectAction = TreeNodeSelectAction.None;
            //    treeLpu.Nodes.Add(nodeRegion);
            //    treeLpu.CollapseAll();
            //}
        }

        //protected void Page_Init(object sender, EventArgs e)
        //{

        //    HtmlGenericControl js = new HtmlGenericControl("script");
        //    js.Attributes["type"] = "text/javascript";
        //    js.Attributes["src"] = "/Scripts/link.js";
        //    Page.Header.Controls.Add(js);

        //}

    }
}